<template>
  <div class>
    <crumbs attribute1="商品管理" attribute2="商品列表"></crumbs>
    <el-card class="box-card">
      <el-input class="elinput" v-model="str" @keydown.enter.native="searbtn" placeholder="请输入内容">
        <el-button slot="append" icon="el-icon-search" @click="searbtn"></el-button>
      </el-input>

      <el-button type="primary" style="margin-left:20px;" @click="fun()">添加商品</el-button>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column label="#" width="80" type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="500"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="180"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="180"></el-table-column>
        <el-table-column prop="upd_time" label="创建时间" width="130">
          <template slot-scope="scope">{{scope.row.add_time | date}}</template>
        </el-table-column>
        <el-table-column label="操作" width="168">
          <template slot-scope="scope">
            <el-button type="warning" size="mini">编辑</el-button>
            <el-button type="danger" size="mini" @click="del(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getGoodsData, DeleteGoods } from "@/http/api";
export default {
  created() {
    this.getData();
  },

  data() {
    return {
      tableData: [],
      
      pagenum: 1,
      pagesize: 10,
      total: 0,
      str:''
    };
  },
  mounted() {},
  methods: {
    // 搜索
    searbtn() {
      this.getData()
      this.str = ''
    },
    //删除商品数据
    del(id) {
      this.id = id;
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let res = DeleteGoods(this.id);
          this.getData();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //跳转添加页面
    fun() {
      this.$router.push({ path: "/addproduct" });
    },
    handleSizeChange(e) {
      this.pagesize = e;
      this.getData();
    },

    handleCurrentChange(e) {
      this.pagenum = e;
      this.getData();
    },
    async getData() {
      const res = await getGoodsData({
        pagenum: this.pagenum,
        pagesize: this.pagesize,
        query: this.str
      });
      this.tableData = res.goods;
      this.total = res.total;
    }
  },
  components: {}
};
</script>

<style lang='scss' scoped>
.el-table {
  margin: 20px 0;
}
.elinput {
  width: 500px;
}
.box-card {
  margin: 20px 0;
  width: 100%;
}
</style>
